<!--#include file="common/header.html"-->
<body>
    <div class="container-fluid larry-wrapper">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <!--头部搜索-->
                <section class="panel panel-padding">
                    <form class="layui-form" action="/education/admin/freightRule">
                        <input type="hidden" name="_query.template_id" id="queryTemplateId">
                        <input type="hidden" name="orderColunm" value="id">
                        <input type="hidden" name="orderMode" value="asc">
                        <div class="layui-form">
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="_query.keyword" placeholder="关键字">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button lay-submit class="layui-btn" lay-filter="search" id="search">查找</button>
                            </div>
                        </div>
                    </form>
                </section>

                <!--列表-->
                <section class="panel panel-padding">
                    <div class="group-button">
                        <button class="layui-btn layui-btn-small modal-catch" beforeFun="beforeAdd"
                                data-params='{"content":".edit-subcat", "title":"添加","type":"1", "full":"true", "maxmin":"true"}'>
                            <i class="iconfont">&#xe649;</i> 添加
                        </button>
                        <button class="layui-btn layui-btn-small layui-btn-danger ajax-all" data-name="checkbox" data-params='{"url": "/education/admin/freightRule/delete"}'>
                            <i class="iconfont">&#xe626;</i> 删除
                        </button>
                    </div>
                    <div id="list" class="layui-form"></div>

                    <div class="text-right" id="page"></div>
                </section>
            </div>
        </div>
    </div>

    <div class="edit-subcat">
        <form class="layui-form layui-form-pane" action="/education/admin/freightRule/save" id="form1">
            <input type="hidden" name="a.id">
            <input type="hidden" name="a.template_id" id="templateId">
            <input type="hidden" name="regionId" id="regionId">

<!--            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">计价方式</label>
                <div class="layui-input-block">
                    <input type="radio" name="a.cal_type" title="按件" value="1" />
                    <input type="radio" name="a.cal_type" title="按重量" value="2" checked />
                </div>
            </div>-->

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">首重</label>
                <div class="layui-input-inline">
                    <input type="text" name="a.first_value" required jq-verify="required"  class="layui-input ">
                </div>
                <div class="layui-form-mid layui-word-aux">重量单位为kg</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">首价</label>
                <div class="layui-input-inline">
                    <input type="text" name="a.first_money" required jq-verify="required"  class="layui-input ">
                </div>
                <div class="layui-form-mid layui-word-aux">价格单位为元</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">续重</label>
                <div class="layui-input-inline">
                    <input type="text" name="a.second_value" required jq-verify="required"  class="layui-input ">
                </div>
                <div class="layui-form-mid layui-word-aux">重量单位为kg</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">续价</label>
                <div class="layui-input-inline">
                    <input type="text" name="a.second_money" required jq-verify="required"  class="layui-input ">
                </div>
                <div class="layui-form-mid layui-word-aux">价格单位为元</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">设置为默认规则</label>
                <div class="layui-input-block">
                    <input type="radio" name="a.is_default" title="否" value="0" checked />
                    <input type="radio" name="a.is_default" title="是" value="1" />
                </div>
            </div>

            <div id="xtree1"
                 style="width:400px;border:1px solid black;padding: 10px 20px 25px 5px;margin: 20px"></div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" jq-submit jq-filter="submit" before-fun="beforeSubmit();">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</body>
<script id="list-tpl" type="text/html"
        data-params='{"url":"/education/admin/freightRule","dataName":"freightRuleData","pageid":"#page", "method":"post", "data":"orderColunm=create_time&orderMode=desc"}'>
    <table id="example" class="layui-table lay-even" data-name="freightRuleData" data-tplid="list-tpl">
        <thead>
        <tr>
            <th width="30"><input type="checkbox" id="checkall" data-name="checkbox" lay-filter="check"
                                  lay-skin="primary"></th>
            <th>首重(kg)</th>
            <th>首价(元)</th>
            <th>续重(kg)</th>
            <th>续价(元)</th>
            <th>是否默认</th>
            <th>区域</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.list, function(index, item){ }}
        <tr>
            <td><input type="checkbox" name="checkbox" value="{{ item.id}}" lay-skin="primary"></td>
            <td>{{ item.first_value||0 }} {{#if (item.cal_type == 1){ }} 件
                {{# } else if(item.cal_type == 2) {  }} kg  {{# } }}
            </td>
            <td>{{ item.first_money||0 }}元</td>
            
            <td>{{ item.second_value||0 }} {{#if (item.cal_type == 1){ }} 件
                {{# } else if(item.cal_type == 2) {  }} kg  {{# } }}
            </td>
            <td>{{ item.second_money||0 }}元</td>
            
            <td>
                {{#if (item.is_default == 1){ }} 是 {{# } else { }} 否 {{# } }}
            </td>
            <td>{{ item.region }}</td>
            <td>{{# if(item.create_time == null){ }} {{#  } else { }} {{ item.create_time}}  {{#  } }}</td>

            <td>
                <button class="layui-btn layui-btn-mini" onclick="viewDetail('{{item.id}}');">
                    <i class="iconfont">&#xe653;</i>编辑
                </button>
                <!--<button class="layui-btn layui-btn-mini layui-btn-danger ajax"
                        data-list='{"key":"id={{ item.id}}","msg":true,"render":"true","action":"del"}'
                        data-params='{"url": "/education/admin/freightRule/delete/{{item.id}}","data":"", "method":"post", "confirm":"true"}'>
                    <i class="iconfont">&#xe626;</i>删除
                </button>-->
            </td>
            </td>
        </tr>
        {{# }); }}
        </tbody>

    </table>
</script>
<script src="../js/layui/layui.js"></script>
<script type="text/javascript" src="../js/lib/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/lib/rest.js"></script>
<script type="text/javascript" src="../js/lib/layui-xtree.js"></script>
<script type="text/javascript" src="../../jsfile/jquery/jquery.form.js"></script>
<!--#include file="common/version.html"-->
<script>
    var xtree1, jsonData;

    var ids = getQueryString('ids');
    layui.use('user_list');
    function initData(){
        var $v = '{"url":"/education/admin/freightRule?_query.template_id='+ ids
            +'","dataName":"freightRuleData","pageid":"#page", "method":"post", "data":"orderColunm=id&orderMode=asc"}';
        $("#list-tpl").attr('data-params', $v);
        $('#queryTemplateId').val(ids);
    }

    function beforeAdd(obj){
        initTree('');
        $('#form1').attr('action', '/education/admin/freightRule/save');
    }

   //https://blog.csdn.net/xianglikai1/article/details/79032278 | http://acmeworker.com/BlogContent?type=1038
    function initTree(ruleId) {
        layui.use(['form'], function () {
            var form = layui.form;
            restGet('/education/admin/freightRule/treeData/'+ruleId, null, null, function(rs){
                jsonData = JSON.stringify(rs.data).replace(/"name"/g, '"title"')
                    .replace(/"operator"/g, '"checked"')
                    .replace(/"id"/g, '"value"')
                    .replace(/"true"/g, 'true')
                    .replace(/"false"/g, 'false')
                    .replace(/"children"/g, '"data"');
                jsonData = $.parseJSON(jsonData);
                console.log(jsonData);
                xtree1 = new layuiXtree({elem: 'xtree1', form: form, data: jsonData});
                xtree1.render();
                new form().render();
            });
        });
    }

    function viewDetail(ids){
        var index = layer.open({
            type: 1,
            title:'编辑',
            area:'600px',
            content: $('.edit-subcat')
        });
        layer.full(index);
        $('#form1').attr('action', '/education/admin/freightRule/update/' + ids);
        restGet('/education/admin/freightRule/view/' + ids, null, $('#form1'), function(rs){
        });
        initTree(ids);
    }

    /**
     * 区域选择
     */
    function beforeSubmit() {
        var arr = [];
        $.each(xtree1.GetAllChecked(), function (i, n) {
            arr.push(n.getAttribute('value'));
        })
        $('#regionId').val(arr.join(','));

        $('#templateId').val(ids);
    }

    /*var data = [
        {
            title: "节点1", value: "jd1", data: [
                { title: "节点1.1", checked: true, disabled: true, value: "jd1.1", data: [] }
                , { title: "节点1.2", value: "jd1.2", checked: true, data: [] }
                , { title: "节点1.3", value: "jd1.3", disabled: true, data: [] }
                , { title: "节点1.4", value: "jd1.4", data: [] }
            ]
        }
        , {
            title: "节点2", value: "jd2", data: [
                { title: "节点2.1", value: "jd2.1", data: [] }
                , { title: "节点2.2", value: "jd2.2", data: [] }
                , { title: "节点2.3", value: "jd2.3", data: [] }
                , { title: "节点2.4", value: "jd2.4", data: [] }
            ]
        }
        , { title: "节点3", value: "jd3", data: [] }
        , { title: "节点4", value: "jd4", data: [] }
    ];*/
/*带全部参数的完整用法
var xtree3 = new layuiXtree({
       elem: 'xtree3'                  //必填
       , form: form                    //必填
       , data: 'server/xtreedata2.ashx' //必填
       , isopen: false  //加载完毕后的展开状态，默认值：true
       , ckall: true    //启用全选功能，默认值：false
       , ckallback: function () { } //全选框状态改变后执行的回调函数
       , icon: {        //三种图标样式，更改几个都可以，用的是layui的图标
           open: "图标代号"       //节点打开的图标
           , close: "图标代号"    //节点关闭的图标
           , end: "图标代号"      //末尾节点的图标
       }
       , color: {       //三种图标颜色，独立配色，更改几个都可以
           open: "#EE9A00"        //节点图标打开的颜色
           , close: "#EEC591"     //节点图标关闭的颜色
           , end: "#828282"       //末级节点图标的颜色
       }
       , click: function (data) {  //节点选中状态改变事件监听，全选框有自己的监听事件
           console.log(data.elem); //得到checkbox原始DOM对象
           console.log(data.elem.checked); //开关是否开启，true或者false
           console.log(data.value); //开关value值，也可以通过data.elem.value得到
           console.log(data.othis); //得到美化后的DOM对象
       }
});
 */

</script>

</html>